﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformText4
{
    public partial class RegisterForm : Form
    {
        public RegisterForm()
        {
            InitializeComponent();
        }

        private void RegisterForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (InputUserName.Text == "" || inputPassword.Text == "")
            {
                MessageBox.Show("请输入用户名、密码！", "警告");
            }

            else
            {
                SqlConnection conn = new SqlConnection("server=LAPTOP-NT2O3C2R\\SQLEXPRESS;uid=xiaoyou;pwd=123456;database=bbtest");
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from UsersInfo where Name='" + InputUserName.Text.Trim() + "'", conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                var uid = InputUserName.Text;
                var pwd = inputPassword.Text;
                var rePwd = okPassword.Text;


                var hasUid = !string.IsNullOrEmpty(uid);
                var hasPwd = !string.IsNullOrEmpty(pwd);
                var eqPwd = pwd == rePwd;
                if (sdr.HasRows)
                {
                    MessageBox.Show("该用户已注册，请使用其他用户名", "提示");
                }

                else if (hasUid && hasPwd && eqPwd)
                {
                    sdr.Close();
                    string myinsert = "insert into UsersInfo values ('" + InputUserName.Text + "','" + inputPassword.Text + "')";
                    SqlCommand mycom = new SqlCommand(myinsert, conn);
                    mycom.ExecuteNonQuery();
                    conn.Close();
                    conn.Dispose();
                    MessageBox.Show("您已注册成功！");

                }
                else
                {
                    MessageBox.Show("注册失败！");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
